<?php
/*
 * @Homepage for application
 * @verion 1.0
 * @author Roshan
 */
class Application extends base {
	
	protected $_view;
	
	public function __construct() {
		$this->setView ();
	}
	public function run($view = '') {
		switch (params::getParam ( 'action' )) {
			
			case 'dashboard' :
				$this->setView ( 'user.phtml' );
				
				//Session::newInstance()->_view();
				$this->dispatch ();
				break;
			
			case 'register' :
				if (user_logged_in ()) {
					redirect ( 'user/dashboard/' );
				}
				$error = false;
				$error_message = array ();
				if (isset ( $_POST ['submit'] )) {
					$args = array ();
					
					/*if ($_POST ['first_name'] == '') {
						$error = true;
						$error_message [] = 'Invalid First name';
					}
					if ($_POST ['last_name'] == '') {
						$error = true;
						$error_message [] = 'Invalid Last name';
					}*/
					
					if ($_POST ['email'] == '' || ! filter_var ( $_POST ['email'], FILTER_VALIDATE_EMAIL )) {
						$error = TRUE;
						$error_message [] = 'Invalid email used';
					}
					
					if (Model_Mapper_User::newInstance ()->getByEmail ( $_POST ['email'] )) {
						$error = true;
						$error_message [] = 'Emal has already been used';
					}
					
					if (! ($_POST ['password'] == '' || strlen ( $_POST ['password'] ) < 6)) {
						
						$args ['password'] = sha1 ( $_POST ['password'] );
					
					} else {
						$error = true;
						$error_message [] = 'Password too short';
					}
					
					$args ['email'] = $_POST ['email'];
					$user = new Model_User ( $args );
					if (! $error) {
						$user->save ();
						$user->sendActivationEmail();
						redirect ( site_url () );
					} else {
						Session::newInstance ()->_setMessage ( 'register_error', $error_message, 'error' );
						$this->setView ( 'user-login.phtml' );
						$this->dispatch ();
					}
				
				} else {
										
					$this->setView ( 'user-login.phtml' );
					$this->dispatch ();
				}
				
				// send user to registration page
				

				break;
			case 'sign-in' :
				// check if logged in redirect to dashboard
				if (user_logged_in ()) {
					redirect ( 'user/dashboard/' );
				}
				/*if (preference::newInstance ()->getPreference ( 'USE_SSL', 'SITE' )) {
					// redirect to ssl
					if (! $_SERVER ['https']) {
						redirect ( 'user/sign-in/', 'https://' );
					}
				}*/
				if (isset ( $_POST ['submit'] )) {
					// login form submited now authenticate the user
					$username = trim ( $_POST ['username'] );
					$password = $_POST ['password'];
					$result = Model_Mapper_User::newInstance ()->getByEmail ( $username );
					
					if ($result) {
							
						// check active
						if (! $result->getActive ()) {
							$result->sendActivationEmail();
						}
						if (! $result->getEnabled ()) {
							// show user has been disabled by admin message
						}
						
						// user exists check for password
						if (strcmp ( sha1 ( $password ), $result->getPassword () ) === 0) {
							Session::newInstance ()->_set ( 'current_user', $result );
							redirect ( 'user/dashboard/' );
						} else {
							$this->setView ( 'user-login.phtml' );
							$this->dispatch ();
						}
					} else {
						// user does not exists
						$this->setView ( 'user-login.phtml' );
						$this->dispatch ();
					}
				
				} else {
					// show login page   
					$this->setView ( 'user-login.phtml' );
					$this->dispatch ();
				}
				
				break;
			case 'sign-out' :
				session_destroy ();
				redirect ( 'user/sign-in/' );
				break;
			case 'activate':
				$secret=Params::getParam('secret');
				$user=Model_Mapper_User::newInstance()->getBySecret($secret);
				if($user){
					$user->activate();
					$user->save();
					$user->sendActivatedEmail(); // send email about account is activated.
				}
				redirect('user/sign-in/');
				break;
				
		}
	
	}

}